package com.zq.api.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.zq.common.annotation.Log;
import com.zq.common.annotation.RepeatSubmit;
import com.zq.common.core.domain.Result;
import com.zq.common.enums.BusinessType;
import com.zq.common.enums.OperatorType;
import com.zq.common.exception.base.BaseException;
import com.zq.common.utils.RegexUtils;
import com.zq.common.utils.StringUtils;
import com.zq.server.admin.service.ISysActivityNotifyService;
import com.zq.server.api.domain.dto.OrdersPaymentDTO;
import com.zq.server.api.domain.dto.OrdersSubmitDTO;
import com.zq.server.api.domain.dto.phoneVerificationTicketDto;
import com.zq.server.api.domain.vo.OrderSubmitVO;
import com.zq.server.api.domain.vo.TicketCheckVo;
import com.zq.server.api.service.ApiOrdersService;
import com.zq.server.core.sevcice.WebSocketServer;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/order")
@Api(tags = "订单相关接口")
@Slf4j
public class ApiorderController {

    @Autowired
    private ApiOrdersService ordersService;

    @Autowired
    private WebSocketServer socketServer;

    @Autowired
    private ISysActivityNotifyService sysActivityNotifyService;

    /**
     * 用户待支付页面
     *
     * @return 待支付订单列表
     */
    @ApiOperation("用获取户待支付集合")
    @GetMapping("unpaid")
    public Result<List<OrderSubmitVO>> getUnpaidOrders() {
        log.info("用户待支付页面请求");
        List<OrderSubmitVO> unpaidOrders = ordersService.getUnpaidOrders();
        return Result.success(unpaidOrders);
    }

    /**
     * @return 获取用户待签到的订单
     */
    @ApiOperation("获取用户待签到的订单")
    @GetMapping("getUserStayTicketCheck")
    public Result<List<TicketCheckVo>> getUserStayTicketCheck() {
        try {
            log.info("获取订单列表");
            List<TicketCheckVo> userOrderList = ordersService.getUserStayTicketCheck();
            return Result.success(userOrderList);
        } catch (BaseException e) {
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation("用户所有订单")
    @GetMapping("getOrderList")
    public Result<List<OrderSubmitVO>> getUserOrderList() {
        try {
            log.info("获取订单列表");
            List<OrderSubmitVO> userOrderList = ordersService.getUserOrderList();
            return Result.success(userOrderList);
        } catch (BaseException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 用户下单
     *
     * @param ordersSubmitDTO 订单提交信息
     * @return 订单提交结果
     */
    @ApiOperation("用户下单接口")
    @PostMapping("/submit")
    @RepeatSubmit(interval = 10000)
    @Log(title = "用户下单" , businessType = BusinessType.INSERT ,operatorType = OperatorType.MOBILE)
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单：{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = ordersService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 用户验票
     *
     * @param orderNumber 订单号
     * @return 修改结果
     */
    @PostMapping("/updateUserTicketState")
    @ApiOperation("修改用户订单验票")
    @RepeatSubmit(interval = 3000)
    @Log(title = "用户订单验票" , businessType = BusinessType.UPDATE ,operatorType = OperatorType.MOBILE)
    public Result updateUserTicketState(@RequestBody String orderNumber) {
        System.out.println(orderNumber);
        Result response = ordersService.updateUserTicketState(orderNumber);
        return response;

    }
    /**
     * 用户根据手机尾号（四位）和订单id进行验票
     * @param dto 包含订单id和手机号的DTO对象
     * @return 验票是否成功
     */
    @PostMapping("/phoneVerificationTicket")
    @RepeatSubmit(interval = 3000 , message ="亲您提交的太快啦")
    @ApiOperation("手机号验票")
    @Log(title = "手机号验票", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE)
    public Result phoneVerificationTicket(@RequestBody  @Validated phoneVerificationTicketDto dto)
    {
        Long oid = dto.getOid();
        String phone = dto.getPhone();
        // 匹配四位数字的正则表达式
        String regex = "^\\d{4}$";
        Boolean isRegex = RegexUtils.Regex(regex, phone);
        if (!isRegex || StringUtils.isNull(oid)) {
            log.error("手机号或订单id无效：, orderId={} - phone={}", oid, phone);
            throw new BaseException("40001", "Invalid phone or orderId");
        }
        Result response = ordersService.phoneVerificationTicket(oid, phone);

        return response;
    }


    /**
     * 订单支付
     *
     * @param ordersPaymentDTO 订单支付信息
     * @return 支付结果
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    @RepeatSubmit(interval = 10000)
    @Log(title = "订单支付" , businessType = BusinessType.INSERT ,operatorType = OperatorType.MOBILE)
    public Result<String> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        try {
            ordersService.payment(ordersPaymentDTO);
            log.info("订单支付成功");
            return Result.success("订单支付成功");
        } catch (Exception e) {
            log.error("订单支付失败：{}", e.getMessage());
            throw  new BaseException("订单支付失败：" + e.getMessage());
        }
    }
}
